package service.impl;

import java.util.Date;

import org.apache.commons.lang3.StringUtils;

import models.Doc;
import models.Doc.Mandatory;
import processor.DocProcessor;
import service.DocService;

public class DocServiceImpl extends AbstractServiceImpl implements DocService {

    protected Doc dbdoc;

    public DocServiceImpl() {
        this.dbdoc = new Doc();
    }

    public DocServiceImpl(Doc doc) {
        this.dbdoc = doc;
    }

    @Override
    public DocProcessor getProcessor() {
        // TODO Auto-generated method stub
        return new DocProcessor(this.dbdoc);
    }

    public Doc getDbdoc() {
        return dbdoc;
    }

    @Override
    public boolean isMandatory() {
        Mandatory mandatory = this.dbdoc.getMandatory();
        if (Mandatory.NO.equals(mandatory))
            return false;
        else
            return true;
    }

    @Override
    public boolean filesWereUploaded() {
        Date statusDate = this.dbdoc.getStatusD();
        String latestVersion = this.dbdoc.getVersion();
        if (statusDate != null && StringUtils.isNotEmpty(latestVersion)) {
            try {
                Long latestVersionTime = Long.valueOf(latestVersion);
                return statusDate.getTime() < latestVersionTime;
            } catch (Exception e) {
                // Invalid version number
            }
        }
        return false;
    }

}
